<!--登录页-->
<template>
  <div style="height:100vh;background-image:linear-gradient(135deg,#999,#eee);overflow:hidden;">
    <el-container style="padding:30px;">
      <el-header>
        <el-row :gutter="10">
          <el-col :span="8"></el-col>
          <el-col :span="12">
            <span style="color:#fff;font-size:60px;font-weight:bold;text-align: center;">连锁水果超市管理系统</span>
          </el-col>
        </el-row>
      </el-header>
      <el-main>
        <el-card style="width: 500px;margin:100px auto 0;">
          <template #header>
            <h3 style="text-align:center;font-size: 30px;margin-bottom: 0;">店铺端登录</h3>
          </template>
          <el-form label-position="top">
            <el-form-item label="店铺名">
              <el-select placeholder="请选择店铺名称" v-model="shopLogin.shopName">
                <el-option v-for="item in shopArr" :label="item.name" :value="item.name"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="密码">
              <el-input placeholder="请输入密码" type="password" v-model="shopLogin.password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" style="width:100%;" @click="login">登录</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import {onMounted, ref} from "vue";
import router from "@/router";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";

const shopArr = ref([]);
const shopLogin = ref({shopName:'',password:''});
onMounted(()=>{
  axios.get(BASE_URL+'/v1/shop/select')
      .then((response)=>{
        if (response.data.code == 2000){
          shopArr.value = response.data.data;
          console.log(shopArr.value);
        }else {
          ElMessage(response.data.msg);
        }
      })
});

const login = ()=>{
  let data = qs.stringify(shopLogin.value);
  //向后端发登录请求
  axios.get(BASE_URL+'/v1/shop/login?'+data)
      .then((response)=>{
        if (response.data.code == 2000){
          localStorage.setItem('loginShop',JSON.stringify(response.data.data));
          router.push('/shopManage');
        }else {
          ElMessage.error(response.data.msg);
          shopLogin.value = {shopName:'',password:''};
        }
      })
}
</script>

<style scoped>
</style>